package main

import (
	_ "gf-app/boot"
	_ "gf-app/router"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/util/gvalid"
	"os"
)

type User struct {
	Uid   int    `json:"uid"`
	Name  string `json:"name"  params:"username" gvalid:"username @required|length:6,30"`
	Pass1 string `json:"pass2" params:"password1"`
	Pass2 string `json:"pass3" params:"password2"`
}

type Object struct {
}

func init() {
	//设置日志路径
	path := g.Cfg().Get("logger.path")
	glog.SetPath(path.(string))

	//对象注册方式，注册路由
	g.Server().BindObject("/object", new(Object))

	//gcron.Add("0 */1 * * * *", func() { glog.Println("Every hour on the half hour") })
	//gcron.Add("* * * * * *",  func() { glog.Println("Every second") }, "second-cron")
	//gcron.Add("@hourly",      func() { glog.Println("Every hour") })
	//gcron.Add("@every 1h30m", func() { glog.Println("Every hour thirty") })
	//g.Dump(gcron.Entries())
}

func main() {
	curPath, _ := os.Getwd()

	s := g.Server()

	//127.0.0.1:8199/hello
	s.BindHandler("/hello", func(r *ghttp.Request) {
		r.Response.Write("hello world")
	})
	//127.0.0.1:8199/math-20/zhang/exam
	s.BindHandler("/{class}-{course}/:name/*act", func(r *ghttp.Request) {
		r.Response.Writeln(r.Get("class"))
		r.Response.Writeln(r.Get("course"))
		r.Response.Writeln(r.Get("name"))
		r.Response.Writeln(r.Get("act"))
	})
	/*s.BindHandler("/:name", func(r *ghttp.Request) {
		r.Response.Writeln(r.Router.Uri)
		r.Response.Writeln(r.Header.Get("Cache-Control"))
	})
	s.BindHandler("/:name/update", func(r *ghttp.Request) {
		r.Response.Writeln(r.Router.Uri)
	})
	s.BindHandler("/:name/:action", func(r *ghttp.Request) {
		r.Response.Writeln(r.Router.Uri)
	})
	s.BindHandler("/:name/*any", func(r *ghttp.Request) {
		r.Response.Writeln(r.Router.Uri)
	})
	s.BindHandler("/:user/list/{field}.html", func(r *ghttp.Request) {
		r.Response.Writeln(r.Router.Uri)
	})*/

	s.BindHandler("GET:/{table}/list/{page}.html", func(r *ghttp.Request) {
		r.Response.WriteJson(r.Router)
	})

	s.BindHandler("/user/:user", func(r *ghttp.Request) {
		r.Response.Writeln(r.GetArray("name"))
		r.Response.Writeln(r.Get("name"))
		r.Response.Writeln(r.GetBody())
		r.Response.Writeln(r.GetBodyString())
		r.Response.Writeln(r.GetJson())
		r.Response.Writeln(r.GetPost("test"))
		r.Response.Writeln(r.GetUrl())

		//r.Response.WriteJson(r.Router)
	})

	s.BindHandler("/user", func(r *ghttp.Request) {
		//pass1:=r.Get("password1")
		//fmt.Println(pass1)

		//127.0.0.1:8199/user?username=testhhahah&password1=a&password2=b&uid=1
		user := new(User)
		r.GetToStruct(user)
		//r.GetQueryToStruct(user)
		//r.GetPostToStruct(user)

		if err := gvalid.CheckStruct(user, nil); err != nil {
			r.Response.WriteJsonExit(err.Maps())
		} else {
			r.Response.WriteJsonExit(user)
		}

	})

	//是否允许列出webserver主目录的文件列表
	s.SetIndexFolder(true)

	//设置静态服务路径
	s.SetServerRoot(curPath + "/public")
	//s.AddSearchPath("/mnt/hgfs/wwwroot/oa_site/new_src/public")

	//设置多端口访问
	//s.SetPort(8000,8199)
	g.Server().Run()
}

func (o *Object) Index(r *ghttp.Request) {
	r.Response.WriteJsonExit(r.GetUrl())
}

func (o *Object) Show(r *ghttp.Request) {
	r.Response.WriteJsonExit(r.GetUrl())
}
